﻿Imports System.Net
Imports System.Threading
Imports Sy.UI
Imports Sy.UI.Cef

Public Class Form4
    Public WithEvents CefWeb2 As CefBrowser
    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            brandlist.Items.Add("ip51.com")
            brandlist.Items.Add("trademarkia.com")
            brandlist.Items.Add("tmdn.org")
            brandlist.SelectedIndex = 0
            brandlistname = "ip51.com"
            Dim url = ""
            'Dim url = "https://www.tmdn.org/tmview/#/tmview"
            If Form1.usecookie.Checked Then
                url = Form1.listurl.Text
            Else
                url = "https://www.ip51.com/brand/search"
            End If
            CefWeb2 = Sy.UI.CefBrowser.LoadBrowser(Pnl_Web, url)
        Catch ex As Exception
            TextBox1.Text = "页面载入失败！请重新尝试"
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim url = "https://www.tmdn.org/tmview/#/tmview"
            If brandlist.Text = "trademarkia.com" Then
                url = "https://www.trademarkia.com/trademarks-search.aspx?tn=gucci"
                brandlistname = "trademarkia.com"
            End If
            If brandlist.Text = "ip51.com" Then
                url = "https://www.ip51.com/brand/search"
                brandlistname = "ip51.com"
            End If
            If brandlist.Text = "tmdn.org" Then
                url = "https://www.tmdn.org/tmview/#/tmview"
                brandlistname = "tmdn.org"
            End If
            'Pnl_Web.Controls.Remove(CefWeb4)
            CefWeb2.Load(url)
        Catch ex As Exception

        End Try
    End Sub
    Sub CefWeb2_DocumentCompleted(sender As CefBrowser, DocumentCompletedEventArgs As DocumentCompletedEventArgs) Handles CefWeb2.DocumentCompleted
        'Dim page = DocumentCompletedEventArgs.GetDomHtml()
        Try
            Dim lstcookie = CefWeb2.GetCookie
            Dim b As String
            For Each s In lstcookie
                b &= s.Name & "=" & s.Value & ";"
            Next
            TextBox1.Text = b
        Catch ex As Exception
            MsgBox("请先等待网站载入完成！")
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            Dim url = "https://www.amazon.com/"
            If Version = "米境通亚马逊选品工具" Then
                url = Form3.listurl.Text
            Else
                If Form1.usecookie.Checked Then
                    url = Form1.listurl.Text
                Else
                    url = "https://www." & Form1.country.Text
                End If
            End If
            CefWeb2.Load(url)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            Dim lstcookie = CefWeb2.GetCookie
            Dim b As String
            For Each s In lstcookie
                b &= s.Name & "=" & s.Value & ";"
            Next
            TextBox1.Text = b
            tradecookie = b
            MsgBox("保存成功，可关闭页面执行调用！")
            'Me.Hide()
        Catch ex As Exception
            MsgBox("保存失败，请等待页面载入完成后获取！")
        End Try
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Try
            Dim html As String
            If brandlistname = "trademarkia.com" Then
                'Pnl_Web.Controls.Remove(CefWeb)
                Dim url = "https://www.trademarkia.com/trademarks-search.aspx?tn=gucci"
                CefWeb2.Load(url)
                Dim page As String = ""
                page = waitfor(CefWeb2, url, "txt txt-lg")
                If InStr(page.ToLower, "here are your search results") > 0 Then
                    MsgBox("查询成功")
                Else
                    MsgBox("查询失败")
                End If
            End If
            If brandlistname = "ip51.com" Then
                Dim formdata = "{""keyWords"":[""aaaaaaaagggggg""],""sources"":[],""ncl"":[],""Keyword"":""aaaaaaaagggggg"",""status"":""0""}"
                html = getnewhtml("https://www.ip51.com/api/brand/search", "POST", formdata, tradecookie, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36", "https://www.ip51.com/brand/search", "*/*", "application/json")
                Console.WriteLine(html)
                If InStr(html, "{""brands"":[]}") > 0 Then
                    MsgBox("查询成功！")
                Else
                    MsgBox("查询失败！")
                End If
            End If
            If brandlistname = "tmdn.org" Then
                html = getnewhtml("https://www.tmdn.org/tmview/api/search/autocomplete/basicSearch?text=gucci", "GET", "", tradecookie)
                If html <> "" Then
                    MsgBox("查询成功！")
                Else
                    MsgBox("查询失败！")
                End If
            End If
            TextBox1.Text = html
        Catch ex As Exception
            MsgBox("查询失败！")
        End Try
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Try
            Dim lstcookie = CefWeb2.GetCookie
            Dim b As String
            For Each s In lstcookie
                b &= s.Name & "=" & s.Value & ";"
            Next
            TextBox1.Text = b
            amazoncookie = b
            MsgBox("保存成功，可关闭页面执行调用！")
        Catch ex As Exception
            MsgBox("保存失败，请等待页面载入完成后获取！")
        End Try
    End Sub

End Class